package com.jobpass.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.jobpass.realm.UserRealm;
import com.jobpass.shiro.cache.RedisCacheManager;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean()
    {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(getDefaultWebSecurityManager());
        //添加shiro内置过滤器
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/company/login", "anon");
        filterMap.put("/admin/index", "perms[admin,manager]");
        //静态资源需要释放，springboot默认把所有的静态资源都映射到static
        filterMap.put("/admin/*","authc");
        filterMap.put("/images/**","anon");
        filterMap.put("/css/**","anon");
        filterMap.put("/js/**","anon");
        filterMap.put("/*","authc");
        filterMap.put("/**","authc");
        //登录页面
        shiroFilterFactoryBean.setLoginUrl("/toLogin");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }

    /**
     * 2、创建DefaultWebSecurityManager
     * @return
     */
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager()
    {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(getRealm());
        return securityManager;
    }

    /**
     * 1、创建Realm
     * @return
     */
    @Bean
    public UserRealm getRealm()
    {
        UserRealm userRealm = new UserRealm();
        //修改凭证校验匹配器
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //设置加密算法
        credentialsMatcher.setHashAlgorithmName("MD5");
        //设置散列次数
        credentialsMatcher.setHashIterations(1024);
        userRealm.setCredentialsMatcher(credentialsMatcher);

//        开启缓存管理器 (应用内缓存)
//        userRealm.setCacheManager(new EhCacheManager());
//        userRealm.setCachingEnabled(true); //开启全局缓存
//        userRealm.setAuthenticationCachingEnabled(true); //开启认证缓存
//        userRealm.setAuthenticationCacheName("authenticationCache");
//        userRealm.setAuthorizationCachingEnabled(true); //开启授权缓存
//        userRealm.setAuthorizationCacheName("authorizationCache");

//        //redis实现
        userRealm.setCacheManager(new RedisCacheManager());
        userRealm.setCachingEnabled(true); //开启全局缓存
        userRealm.setAuthenticationCachingEnabled(true); //开启认证缓存
        userRealm.setAuthenticationCacheName("authenticationCache");
        userRealm.setAuthorizationCachingEnabled(true); //开启授权缓存
        userRealm.setAuthorizationCacheName("authorizationCache");
        return userRealm;
    }

    /**
     * 用于thymeleaf和shiro标签配合使用
     * @return
     */
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}
